<template>
  <van-loading color="#1989fa" v-if="loading" />
  <van-icon
    :color="is_like === 1 ? '#3296fa' : '#777'"
    :name="is_like === 1 ? 'good-job' : 'good-job-o'"
    @click="onAttitude"
    v-else
  />
</template>
<script>
import { addLike, deleteLike } from "@/api/article";
export default {
  props: {
    is_like: {
      type: Number,
      required: true
    },
    artId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data() {
    return {
      loading: false
    };
  },
  methods: {
    async onAttitude() {
      // 判断当前是否登录
      if (!this.$store.state.user) {
        this.$toast("请先登录");
        return this.$router.push("/login?redirect=" + this.$route.path);
      }
      this.loading = true;
      try {
        if (this.is_like === 1) {
          // 当前是收藏状态，要取消收藏
          await deleteLike(this.artId);
        } else {
          await addLike(this.artId);
        }
        this.$emit("update:is_like", this.is_like === 1 ? 0 : 1);
        this.$toast.success(this.is_like === 1 ? "取消点赞成功" : "点赞成功");
      } catch (error) {
        this.$toast.fail(this.is_like === 1 ? "取消点赞失败" : "点赞失败");
      }
      this.loading = false;
    }
  }
};
</script>
<style lang="less" scoped></style>
